#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMessageBox>
#include<QDebug>
#include<QtSql/QSqlDatabase>
#include<QtSql/QSqlQuery>
#include<QDesktopWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QAction* backAction = new QAction( tr("Exit"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );
   // this->setStyleSheet("background-color:black;");

    QRect q=QApplication::desktop()->availableGeometry(this);
    if(q.top()==0)
    {
        if(q.height()==240)
             ui->scrollArea->setGeometry(0,0,(q.width()),(198));
        else if(q.height()==640)
             ui->scrollArea->setGeometry(0,0,(q.width()),(487));
        else if(q.height()==360)
             ui->scrollArea->setGeometry(0,0,(q.width()),(270));
        else if(q.height()==320)
             ui->scrollArea->setGeometry(0,0,(q.width()),(235));
        else
        {
            int h=(q.height()/10.0)*2.5;
             ui->scrollArea->setGeometry(0,0,(q.width()),(q.height()-h));
        }


    }
    else
        ui->scrollArea->setGeometry(0,0,(q.width()),(q.height()));

    ui->listWidget->setEditFocus(true);
    qDebug()<<q;
    qDebug()<<q.width();
   qDebug()<<q.height();
    int fontSize=10;
     //Main Screen options
    //QIcon(":/myresources/images/img1.png")
    item1=new QListWidgetItem("Expense",ui->listWidget,QListWidgetItem::UserType);
    item1->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));

    item2=new QListWidgetItem("Budget",ui->listWidget,QListWidgetItem::UserType);
    item2->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));

    item3=new QListWidgetItem("Account",ui->listWidget,QListWidgetItem::UserType);
    item3->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));

    item4=new QListWidgetItem("Import",ui->listWidget,QListWidgetItem::UserType);
    item4->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));

    item5=new QListWidgetItem("Export",ui->listWidget,QListWidgetItem::UserType);
    item5->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));

    item6=new QListWidgetItem("Exit",ui->listWidget,QListWidgetItem::UserType);
    item6->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));


    if(db.db_connect())
    {
        QStringList tables=QSqlDatabase::database().tables(QSql::Tables);
        if((!tables.contains("account_table"))||(!tables.contains("category_table"))||(!tables.contains("expense_table")))
        {//If no table found, then create table and add default categories and accounts
        if(db.db_category_table())
        {
                db.db_category_insert("Food");
                db.db_category_insert("Clothing");
                db.db_category_insert("Education");
                db.db_category_insert("Bills");
                db.db_category_insert("Medicine");

            if(db.db_account_table())
            {
                db.db_account_insert("Bank",1);
                db.db_account_insert("Credit card",1);

                if(db.db_expense_table())
                {
                    QSqlQuery q;
                    if(q.exec("PRAGMA foreign_keys = ON;"))
                    {
                        qDebug()<<"created all";
                    }
                }
            }
        }
    }
        qDebug()<<"Disconnecting";
        db.db_close();
    }
    else
    {
        QMessageBox::information(0,"Error!","Error in opnening database, exiting");
        exit(0);
    }
   }

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_listWidget_itemActivated(QListWidgetItem* item)
{
    if(item==item1)
    {
       expense *exp=new expense;
       exp->setWindowTitle("Expense Options");
       exp->show();
    }
    if(item==item2)
    {
       (new Budget)->show();

    }
    else if(item==item3)
    {
        account *acc=new account;
        acc->setWindowTitle("Account Options");
        acc->show();
    }
    else if(item==item4)
    {
        (new Import)->show();
    }
    else if(item==item5)
    {
        (new Export)->show();
    }
    else if(item==item6)
    {
        exit(0);
    }
}
void MainWindow::back()
{
    exit(0);
}
